package com.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DynamicTest {
	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		//2.获取sqlSession实例，能直接执行已经映射的sql语句
		SqlSession sqlSession = sqlSessionFactory.openSession();

		EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);

		selectEmpsSection(mapper);
		//getEmpsByConditionIf(mapper);
		//getEmpsByConditionChoose(mapper);
		//getEmpsByConditionForeach(mapper);
		//updateEmpById(mapper);
		// insertBatchEmps(mapper);


		sqlSession.close();
	}

	private static void selectEmpsSection(EmployeeMapperDynamicSQL mapper) {
		List<Employee> empsBySection = mapper.getEmpsBySection(1);
		System.out.println(empsBySection);
	}

	private static void insertBatchEmps(EmployeeMapperDynamicSQL mapper) {
		List<Employee> lst = new ArrayList();
		lst.add(new Employee(null,"hel1","q@qq.com","1",new Department(1)));

		mapper.addEmpsByForeach(lst);
	}

	private static void getEmpsByConditionForeach(EmployeeMapperDynamicSQL mapper) {
		List<Employee> empsByConditionForeach = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3, 7, 9));
		empsByConditionForeach.forEach(System.out::println);
	}

	private static void updateEmpById(EmployeeMapperDynamicSQL mapper) {
		Employee employee = new Employee();
		employee.setId(1);
		employee.setGender("1");
		employee.setLastName("cccc");
		mapper.updateEmps(employee);
	}

	private static void getEmpsByConditionChoose(EmployeeMapperDynamicSQL mapper) {
		Employee employee = new Employee();
		employee.setLastName("hehe");
		List<Employee> empsByConditionChoose = mapper.getEmpsByConditionChoose(employee);
		empsByConditionChoose.forEach(System.out::println);
	}

	private static void getEmpsByConditionIf(EmployeeMapperDynamicSQL mapper) {
		Employee employee = new Employee();
//		employee.setEmail("d");
//		employee.setGender("0");
		employee.setLastName("h");

		List<Employee> empsByConditionIf = mapper.getEmpsByConditionIf(employee);
		System.out.println(empsByConditionIf);
	}

}
